/* Yet Another Experimental Software Renderer
 *
 * Copyright (c) 2010 Pawel "Hery" Dziepak
 */

#ifndef PARSER_H_
#define PARSER_H_

#include "tokenizer.h"
#include "executor.h"

#include <map>

namespace yasl {
	class parser {
	public:
		parser(tokenizer *t) : last_f(1), last_d(1), token(t), in_function(false) { }

		bool parse(executor *);

	protected:
		int new_fname() {
			return last_f++;
		}

		int new_dname() {
			return last_d++;
		}

		int last_f;
		int last_d;

		int get_fname(const std::string &name) {
			std::map<std::string, int>::iterator itr = f_names.find(name);
			if (itr == f_names.end()) {
				int fname = new_fname();
				f_names[name] = fname;
				return fname;
			}

			return itr->second;
		}

		int get_dname(const std::string &name) {
			std::map<std::string, int>::iterator itr = d_names.find(name);
			if (itr == d_names.end()) {
				int dname = new_dname();
				d_names[name] = dname;
				return dname;
			}

			return itr->second;
		}

		executor::var expression();
	private:
		tokenizer *token;

		std::map<std::string, int> f_names;
		std::map<std::string, int> d_names;

		executor::function func;
		bool in_function;

		std::vector<executor::action> act_stack;
	};
}

#endif